package com.weishi.project.coach.controller;

import com.weishi.common.utils.StringUtils;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.project.training.domain.TrainingDay;
import com.weishi.project.training.domain.TrainingGroup;
import com.weishi.project.training.domain.TrainingGroupChannel;
import com.weishi.project.training.service.ITrainingDayService;
import com.weishi.project.training.service.ITrainingGroupChannelService;
import com.weishi.project.training.service.ITrainingGroupService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 教练员端 训练计划
 * zoulj 2021-07-27
 */
@RestController
@RequestMapping("/coach/traningPlan/")
@CrossOrigin("*")
public class TraningPlanController {

    @Autowired
    private ITrainingDayService trainingDayService;

    @Autowired
    private ITrainingGroupService trainingGroupService;

    @Autowired
    private ITrainingGroupChannelService trainingGroupChannelService;

    /**
     * 今日训练内容
     */
    @ApiOperation(value = "训练课目类")
    @ApiImplicitParams({@ApiImplicitParam(name = "day", value = "历史时间", paramType = "path", required = false)})
    @GetMapping("/todayTrainingContent")
    public AjaxResult todayTrainingContent(HttpServletRequest request) throws Exception {
        String strdate = request.getParameter("day");
        if(StringUtils.isEmpty(strdate)){
            Date date = new Date();
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            strdate = sf.format(date);
        }
        List<TrainingDay> list = trainingDayService.selectTrainingDayListByDay(strdate);
        List<Map<String, Object>> rets = new ArrayList<>();
        for (TrainingDay day : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", day.getId());
            map.put("fourSeasons", day.getFourSeasons());
            map.put("firstLevel", day.getFirstLevel());
            map.put("secondLevel", day.getSecondLevel());
            map.put("kilometers", day.getKilometers());
            map.put("idealKilometers", day.getIdealKilometers());
            map.put("numberGroups", day.getNumberGroups());
            map.put("gapTime", day.getGapTime());
            map.put("strength", day.getStrength());
            map.put("score", String.valueOf(day.getScore()));
            map.put("startTime", day.getStartTime());
            map.put("endTime", day.getEndTime());
            map.put("firstLevelName", day.getFirstLevelName());
            map.put("secondLevelName", day.getSecondLevelName());
            rets.add(map);
        }
        return AjaxResult.success(rets);
    }

    /**
     * 查询航道信息
     * @param trainingDayId
     * @return
     * @throws Exception
     */
    @GetMapping("/qryTrainingGroup")
    public AjaxResult qryTrainingGroup(@RequestParam Integer trainingDayId) throws Exception {
        List<TrainingGroup> trainingGroups = trainingGroupService.selectTrainingGroupListByTrainingDayId(trainingDayId);
        int index = 1;
        for(TrainingGroup trainingGroup : trainingGroups){
            trainingGroup.setGroupName("第"+ index++ +"组");
        }
        return AjaxResult.success(trainingGroups);
    }

    /**
     * 查询航道信息
     * @param groupId
     * @return
     * @throws Exception
     */
    @GetMapping("/qryTrainingChannel")
    public AjaxResult qryTrainingChannel(@RequestParam Integer groupId) throws Exception {
        List<TrainingGroupChannel> trainingGroupChannels = trainingGroupChannelService.getTrainingGroupChannelByGroupId(groupId,true);

        return AjaxResult.success(trainingGroupChannels);
    }

}
